//
//  ZYCTeachingTeachLearningDetailCell.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/31.
//

#import "ZYCTeachingTeachLearningDetailCell.h"
#import "ZYCTeachLearningDetailModel.h"
#import "ZYCTeachingLearnDetailCellView.h"
#import "NSString+Size.h"
@interface ZYCTeachingTeachLearningDetailCell ()
@property (nonatomic, strong) UILabel *questionLabel;
@property (nonatomic, strong) UILabel *line;
@property (nonatomic, strong) ZYCTeachingLearnDetailCellView *lastChoiceView;
@property (nonatomic, strong) NSMutableArray * choiceViewArray;


@end
@implementation ZYCTeachingTeachLearningDetailCell


- (NSMutableAttributedString *)returnFuwenBen:(NSString *)string{
    NSMutableAttributedString *attriString = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange rang0 = NSMakeRange(0, self.model.sortNum.length);
    [attriString addAttribute:NSFontAttributeName value:kCustomFont(@"PingFangSC-Medium", 16.0f) range:rang0];
    NSRange rang1 = NSMakeRange(self.model.sortNum.length + 1, self.model.subjectTypeStr.length + 1);
    [attriString addAttribute:NSForegroundColorAttributeName value:HEX_RGB(@"#39A85B") range:rang1];
    return attriString;;
    
}

- (void)setModel:(ZYCDetailSubjectModel *)model{
    _model = model;
    _questionLabel.attributedText = [self returnFuwenBen:[NSString stringWithFormat:@"%@、%@ %@",model.sortNum,model.subjectTypeStr,model.subjectTitle]];
    

    for (ZYCTeachingLearnDetailCellView *choiceView in self.choiceViewArray) {
        [choiceView removeFromSuperview];
    }

    CGFloat margin = 0.0f;
    for (NSInteger i = 0; i < model.subjectItemVoList.count; i++) {
        ZYCDetailAnswerModel *m = model.subjectItemVoList[i];
        ZYCTeachingLearnDetailCellView *choiceView = [[ZYCTeachingLearnDetailCellView alloc] init];
        choiceView.model = m;
        CGSize titleSize =  [m.content sizeWithFont:kCustomFont(@"PingFangSC-Light", 16.0f) constrainedToWidth:self.contentView.frame.size.width - 49];
        m.titleHeight = titleSize.height;
        if (i == 0) {
            margin = 15.0f;
        }else{
            margin += m.titleHeight;
        }
        choiceView.subjectType = [model.subjectType integerValue];
        choiceView.selected  = m.checkFlag;
        [self.contentView addSubview:choiceView];
        [self.choiceViewArray addObject:choiceView];

        if (i == model.subjectItemVoList.count - 1) {
            self.lastChoiceView = choiceView;
            [choiceView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentView).offset(14.0f);
                make.right.equalTo(self.contentView).offset(-14.0f);
                make.top.equalTo(self.line.mas_bottom).offset(margin);
                make.height.mas_equalTo(m.titleHeight);
                make.bottom.equalTo(self.contentView).offset(-15.0f);
            }];
        }else{
            [choiceView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.line.mas_bottom).offset(margin);
                make.left.equalTo(self.contentView).offset(14.0f);
                make.right.equalTo(self.contentView).offset(-14.0f);
                make.height.mas_equalTo(m.titleHeight);
            }];
        }
        // 第一次遍历之后需要将第一个的高度加上 否则会布局异常
        margin += m.titleHeight;
    }
    
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.choiceViewArray = [NSMutableArray arrayWithCapacity:0];
        self.userInteractionEnabled = YES;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.questionLabel];
        [self.contentView addSubview:self.line];
        [self.questionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
        }];
        
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(14.0f);
            make.right.equalTo(self.contentView).offset(-14.0f);
            make.top.equalTo(self.questionLabel.mas_bottom).offset(14.0f);
            make.height.mas_equalTo(0.5f);
        }];
        
    }
    return self;
}


- (UILabel *) questionLabel{
    if (!_questionLabel) {
        _questionLabel = [[UILabel alloc] init];
        _questionLabel.textAlignment = NSTextAlignmentLeft;
        _questionLabel.font = kCustomFont(@"PingFangSC-Light", 16.0f);
        _questionLabel.numberOfLines = 0;
    }
    return _questionLabel;
}

- (UILabel *)line {
    if (!_line) {
        _line = [[UILabel alloc] init];
        _line.backgroundColor = HEX_RGB(@"#EBEBEB");
    }
    return _line;
}



@end
